package snooker.scoreboard.frame;

import snooker.scoreboard.ball.Ball;
import snooker.scoreboard.entity.Type;
import snooker.scoreboard.util.Utils;
import android.util.Log;

public class Score {
	
	private static final String LOG_TAG = "SCORE";
	
	private int playerId;
	private int frameId;
	private Type type;
	private int breakId;
	private Ball ball;
	private int score;
	private long eventTime;
	private boolean isLuckyShot;
	

	public Score(int playerId, int frameId, Type type, int breakId,
			Ball ball, int score, long eventOffSet, boolean isLuckyShot) {
		this.playerId = playerId;
		this.frameId = frameId;
		this.type = type;
		this.breakId = breakId;
		this.ball = ball;
		this.score = score;
		this.eventTime = eventOffSet;
		this.isLuckyShot = isLuckyShot;
		if (isLuckyShot)
			Log.d(LOG_TAG, "Lucky Shot");
	}

	public int getPlayerId() {
		return playerId;
	}

	public int getFrameId() {
		return frameId;
	}

	public Type getType() {
		return type;
	}

	public int getBreakId() {
		return breakId;
	}

	public Ball getBall() {
		return ball;
	}

	public int getScore() {
		return score;
	}

	public long getEventTime() {
		return eventTime;
	}
	
	public boolean isFoul() {
		return Type.Foul.equals(type);
	}
	
	public String getEventTimeString() {
		return Utils.getTimeString(eventTime);
	}
	
	public boolean isLuckyShot() {
		return isLuckyShot;
	}

	@Override
	public String toString() {
		return
			"playerId=" + getPlayerId() + "; " +
			"frameId=" + getFrameId() + "; " +
			"breakId=" + getBreakId() + "; " +
			"type=" + getType() + "; " +
			"ball=" + getBall() + "; " +
			"score=" + getScore() + "; " +
//			"reds=" + getReds() + "; " +
			"eventTime=" + getEventTimeString() + ";" +
			"isLuckyShot=" + (isLuckyShot()?"*":"");
	}
	
	public String getSerializedString() {
		Score state = this;
		return 
			state.getPlayerId() + "," +
			state.getFrameId() + "," + 
			state.getType() + "," + 
			(state.getBreakId() == 0 ? "" : state.getBreakId()) + "," + 
			state.getBall() + "," +
			state.getScore() + "," + 
			state.getEventTimeString() + "," + 
			(state.isLuckyShot() ? "*" : " ");
	}
	
	public String getSerializedStringOld(String playerName) {
		Score state = this;
		return 
			playerName + "," +
			state.getFrameId() + "," + 
			state.getType() + "," + 
			(state.getBreakId() == 0 ? "" : state.getBreakId()) + "," + 
			state.getBall() + "," +
			state.getScore() + "," + 
			state.getEventTimeString() + "," + 
			(state.isLuckyShot() ? "*" : " ");
	}
	
	public static Score getDeserializedScore(String line) {
		Log.i("Score", "line=" + line);
		String[] cells = line.split(",");
		Score state = null;
		if (cells.length == 8 || cells.length == 7) {
			String playerS  = cells[0];
			String frameS 	= cells[1];
			String typeS 	= cells[2];
			String breakS 	= cells[3];
			String ballS 	= cells[4];
			String pointS 	= cells[5];
			String timeS 	= cells[6];
			String luckyS   = " ";
			if (cells.length == 8) {
				luckyS 	    = cells[7];
			}
			
			int pId 		 = Integer.parseInt(playerS);
			int frameId 	 = Integer.parseInt(frameS);
			Type type 		 = Type.valueOf(typeS);
			int breakId 	 = "".equals(breakS) ? 0 : Integer.parseInt(breakS);
			Ball ball 		 = Ball.valueOf(ballS);
			int score		 = Integer.parseInt(pointS);
			String[] s 		 = timeS.split(":");
			long eventOffSet = (Integer.parseInt(s[0]) * 60 + Integer.parseInt(s[1])) * 1000;
			boolean lucky 	 = "*".equals(luckyS);
			state = new Score(pId, frameId, type, breakId, ball, score, eventOffSet, lucky);
		} else {
			Log.i("Score", "SHIT");
		}
		return state;
	}
}
